// miniprogram/pages/applyDetail/applyDetail.js

import url from "../../util/url.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgs: [
      "../../images/apply1.png",
      "../../images/apply4.png",
      "../../images/apply3.png",
      "../../images/apply2.png",
    ],
    titles: [
      "申请运营中心",
      "申请省代",
      "申请市代",
      "申请区代"
    ],
    index:0,
    isCheck:false,
    checkUrl: '../../images/dagouweigouxuan.png',
    userId:'',
    openId:'',
  },

  checkClick:function(e){
     this.setData({
       isCheck: !this.data.isCheck,
       checkUrl: !this.data.isCheck ? '../../images/dagou.png' : '../../images/dagouweigouxuan.png'
     })
  },

  apply:function(e){
    if(this.data.isCheck){
      wx.showLoading({
        title: '',
        mask:true
      })
      let piType = 0;
      if (this.data.index == 0){
        piType = 1
      } else if (this.data.index == 1){
        piType = 11
      } else if (this.data.index == 2){
        piType = 21
      }else{
        piType = 31
      }
      wx.request({
        url: url.apply,
        method:'POST',
        data:{
          merchantNo: this.data.userId,
          appId:'wx5298b0879c8fa8c4',
          openId: this.data.openId,
          piType: piType,
          userId: this.data.userId,
          returnParams: this.data.userId + "," + piType
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: (res) => {
          if(!res.data || res.data.code != 200){
            wx.showModal({
              title: '提示',
              content: res.data.message || res.data,
              showCancel: false
            })
          }else{
            let payObj = JSON.parse(res.data.data.preparePayInfo);
            wx.requestPayment(
              {
                'timeStamp': payObj.timeStamp,
                'nonceStr': payObj.nonceStr,
                'package': payObj.package,
                'signType': payObj.signType,
                'paySign': payObj.paySign,
                'success': res =>{
                  wx.showModal({
                    title: '提示',
                    content: '申请成功',
                    showCancel: false
                  })
                },
                'fail': res => { 
                  wx.showModal({
                    title: '提示',
                    content: '申请失败',
                    showCancel: false
                  })
                },
              })
          }
          
        },
        complete:() => {
          wx.hideLoading()
        }
      })
    }else{
      wx.showToast({
        title: '请先同意代理协议',
        icon: 'none'
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      index : options.id
    })
    wx.getStorage({
      key: 'openId',
      success: (res) =>{
        console.log(res)
        this.setData({
          openId:res.data
        })
      },
    })
    wx.getStorage({
      key: 'userId',
      success: (res) => {
        this.setData({
          userId: res.data
        })
      },
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})